package team;

import java.util.Arrays;
import java.util.Comparator;

public class Team {
    private final String teamName;
    private final TeamMember[] teamMembers; // 包含所有队员的信息

    public Team(String teamName, TeamMember[] teamMembers) {
        this.teamName = teamName;
        this.teamMembers = teamMembers;
    }

    public String getTeamName() {
        return teamName;
    }

    public void printTop50Members() {
        // 复制数组以进行排序
        TeamMember[] sortedMembers = new TeamMember[teamMembers.length];
        System.arraycopy(teamMembers, 0, sortedMembers, 0, teamMembers.length);

        Arrays.sort(sortedMembers, new Comparator<>() {
            @Override
            public int compare(TeamMember o1, TeamMember o2) {
                return Double.compare(o2.getAverageScore(), o1.getAverageScore());
            }
        });

        System.out.println("平均成绩最好的前50名队员信息：");
        for (int i = 0; i < Math.min(50, sortedMembers.length); i++) {
            TeamMember member = sortedMembers[i];
            System.out.println("队员名字：" + member.getName() + ", 年龄：" + member.getAge() + ", 各项目成绩：" + getScoresAsString(member.getScores()) + ", 平均成绩：" + member.getAverageScore());
        }
    }

    private String getScoresAsString(double[] scores) {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < scores.length; i++) {
            sb.append(scores[i]);
            if (i < scores.length - 1) {
                sb.append(", ");
            }
        }
        sb.append("]");
        return sb.toString();
    }
}
